﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;
using Newtonsoft.Json;
using System.Text;


namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AllowancePayController : AdminBaseController
    {
        [AuthorityActionFilter("m_AllowancePay", Order = 1)]
        [LogPageActionFilter("资金拨付", Order = 2)]
        public ActionResult Index(string NSRMC, string FFileNo, string HY_DM, string FEnterprisePropNo, string FFinancePropNo, string IsNoFull)
        {

            string strwhere = "";
            if (NSRMC != null && NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
                ViewData["NSRMC"] = NSRMC;
            }
            if (FFileNo != null && FFileNo != "")
            {
                strwhere += " and FFileNo ='" + FFileNo + "'";
                ViewData["FFileNo"] = FFileNo;
            }
            //if (HY_DM != null && HY_DM != "" && HY_DM != "null")
            //{
            //    strwhere += " and HY_DM='" + HY_DM + "'";
            //    ViewData["HY_DM"] = HY_DM;
            //}
            //if (FEnterprisePropNo != null && FEnterprisePropNo != "" && FEnterprisePropNo != "null")
            //{
            //    strwhere += " and FEnterprisePropNo='" + FEnterprisePropNo + "'";
            //    ViewData["FEnterprisePropNo"] = FEnterprisePropNo;
            //}
            if (FFinancePropNo != null && FFinancePropNo != "" && FFinancePropNo != "null")
            {
                strwhere += " and FFinancePropNo='" + FFinancePropNo + "'";
                ViewData["FFinancePropNo"] = FFinancePropNo;
            }
            //if (IsNoFull != null && IsNoFull != "")
            //{
            //    if (IsNoFull == "Y")
            //    {
            // strwhere += " and  NSRDZDAH  in (select NSRDZDAH from fund.t_Key_NSRXX)";
            //    }
            //    ViewData["IsNoFull"] = IsNoFull;
            //}
            // AllowanceApplicationForm.cs

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            List<Model.AllowanceApplicationForm> codeNameList = new List<Model.AllowanceApplicationForm>();
            ViewData["blcode"] = true;
            BLL.AllowanceApplicationForm codeNameBll = new BLL.AllowanceApplicationForm();
            codeNameList = codeNameBll.GetModelList1(strwhere);
            //foreach (Model.AllowanceApplicationForm c in codeNameList)
            //{
            //    if (c != null)
            //    {
                    
                    
            //        c.FAuditAmount = !string.IsNullOrEmpty(c.FAuditAmount.ToString())?decimal.Parse(c.FAuditAmount.ToString()) / 10000:0;
                 
            //        c.Famount =!string.IsNullOrEmpty(c.Famount.ToString())? (decimal.Parse(c.Famount.ToString()) / 10000).ToString():"";
            //        c.FamountLeave =!string.IsNullOrEmpty(c.FamountLeave.ToString())?  (decimal.Parse(c.FamountLeave.ToString()) / 10000).ToString():"";
            //    }
            //}
            return View(codeNameList);
        }
        /// <summary>
        /// 编码树相关调用方法 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult SpecialAdjustClsListPartial()
        {
            string str = "1=1";
            BLL.AllowancePayEntry codeNameBll = new BLL.AllowancePayEntry();
            List<Model.AllowancePayEntry> codeNameList = new List<Model.AllowancePayEntry>();
            string FApplicationFormID = Session["FApplicationFormID"].ToString();
            if (FApplicationFormID != "" || FApplicationFormID != null)
            {
                str = str + " and FApplicationFormID=" + FApplicationFormID;
            }
            codeNameList = codeNameBll.GetModelList(str);
            //foreach (Model.AllowancePayEntry c in codeNameList)
            //{
            //    if (c != null)
            //    {

            //        c.FAmount =!string.IsNullOrEmpty(c.FAmount.ToString()) ? c.FAmount / 10000:0;
            //    }
            //}
            ViewData["blcode"] = true;
            return PartialView("FormerYearSpecificAdjustListPartial", codeNameList);
        }
        public ActionResult AllowancePayManagerListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            //  string NSRBM = Request.Params["NSRBM"];
            //  string NSRMC = Request.Params["NSRMC"];

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "";
            string NSRMC = Request.Params["NSRMC"];
            string FFileNo = Request.Params["FFileNo"];
            //Request.Params["HY_DM"]
            //Request.Params["FFinancePropNo"]
            string FFinancePropNo = Request.Params["FEnterprisePropNo"];
            //   Request.Params["IsNoFull"]
            if (NSRMC != null && NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
                ViewData["NSRMC"] = NSRMC;
            }
            if (FFileNo != null && FFileNo != "")
            {
                strwhere += " and FFileNo ='" + FFileNo + "'";
                ViewData["FFileNo"] = FFileNo;
            }
            //if (HY_DM != null && HY_DM != "" && HY_DM != "null")
            //{
            //    strwhere += " and HY_DM='" + HY_DM + "'";
            //    ViewData["HY_DM"] = HY_DM;
            //}
            //if (FEnterprisePropNo != null && FEnterprisePropNo != "" && FEnterprisePropNo != "null")
            //{
            //    strwhere += " and FEnterprisePropNo='" + FEnterprisePropNo + "'";
            //    ViewData["FEnterprisePropNo"] = FEnterprisePropNo;
            //}
            if (FFinancePropNo != null && FFinancePropNo != "" && FFinancePropNo != "null")
            {
                strwhere += " and FFinancePropNo='" + FFinancePropNo + "'";
                ViewData["FFinancePropNo"] = FFinancePropNo;
            }
            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            List<Model.AllowanceApplicationForm> codeNameList = new List<Model.AllowanceApplicationForm>();
            ViewData["blcode"] = true;
            BLL.AllowanceApplicationForm codeNameBll = new BLL.AllowanceApplicationForm();
            codeNameList = GetAllowanceApplicationFormListList(strwhere, pageIndex);
            //foreach (Model.AllowanceApplicationForm c in codeNameList)
            //{
            //    if (c != null)
            //    {

            //        c.FAuditAmount = !string.IsNullOrEmpty(c.FAuditAmount.ToString()) ? decimal.Parse(c.FAuditAmount.ToString()) / 10000 : 0;

            //        c.Famount = !string.IsNullOrEmpty(c.Famount.ToString()) ? (decimal.Parse(c.Famount.ToString()) / 10000).ToString() : "";
            //        c.FamountLeave = !string.IsNullOrEmpty(c.FamountLeave.ToString()) ? (decimal.Parse(c.FamountLeave.ToString()) / 10000).ToString() : "";
            //    }
            //}
            return PartialView("AllowancePayManagerListPartial", codeNameList);
        }
        private List<Model.AllowanceApplicationForm> GetAllowanceApplicationFormListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.AllowanceApplicationForm AllowanceApplicationFormBLL = new BLL.AllowanceApplicationForm();
            string fiedls = "FApplicationFormID";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.v_AllowancePay", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FApplicationFormID", out total);
            List<Model.AllowanceApplicationForm> AllowanceApplicationFormList = AllowanceApplicationFormBLL.DataTableToList(ds.Tables[0]);
            //foreach (Model.AllowanceApplicationForm a in AllowanceApplicationFormList)
            //{
            //    a.FApplyAmount = a.FApplyAmount / 10000;
            //    a.FAuditAmount = a.FAuditAmount / 10000;
            //}
            List<Model.AllowanceApplicationForm> AllowanceApplicationFormAllList = new List<Model.AllowanceApplicationForm>();
            total = AllowanceApplicationFormBLL.GetRecordCountv(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                AllowanceApplicationFormAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (AllowanceApplicationFormList.Count > i)
                {
                    AllowanceApplicationFormAllList[((pageIndex - 1) * pageRowSize) + i] = AllowanceApplicationFormList[i];
                }
            }

            return AllowanceApplicationFormAllList;
        }
        public ActionResult AllowancePayEdit(string FApplicationFormID)
        {
            // string strwhere= str;
            //List<Model.AllowanceApplicationForm> codeNameList = new List<Model.AllowanceApplicationForm>();
            // ViewData["FApplicationFormID"] = FApplicationFormID;
            Session["FApplicationFormID"] = FApplicationFormID;
            BLL.AllowanceApplicationForm codeNameBll = new BLL.AllowanceApplicationForm();
            // codeNameList = codeNameBll.GetModelList1(strwhere);
            Model.AllowanceApplicationForm c = new Model.AllowanceApplicationForm();
            c = codeNameBll.GetModel1(FApplicationFormID);

            //c.FAuditAmount = !string.IsNullOrEmpty(c.FAuditAmount.ToString()) ? decimal.Parse(c.FAuditAmount.ToString()) / 10000 : 0;

            //c.Famount = !string.IsNullOrEmpty(c.Famount.ToString()) ? (decimal.Parse(c.Famount.ToString()) / 10000).ToString() : "";
            //c.FamountLeave = !string.IsNullOrEmpty(c.FamountLeave.ToString()) ? (decimal.Parse(c.FamountLeave.ToString()) / 10000).ToString() : "";

            return View("AllowancePayEdit", c);
        }

        public ActionResult FormerYearAllowancePayShow(string FApplicationFormID)
        {
            Session["FApplicationFormID"] = FApplicationFormID;
            string str = "1=1";
            List<Model.AllowancePayEntry> codeNameList = new List<Model.AllowancePayEntry>();
            ViewData["blcode"] = true;

            BLL.AllowancePayEntry codeNameBll = new BLL.AllowancePayEntry();
            if (FApplicationFormID == null || FApplicationFormID == "")
            { }
            else
            {
                str = str + " and FApplicationFormID=" + FApplicationFormID;
                codeNameList = codeNameBll.GetModelList(str);
            }
            //foreach (Model.AllowancePayEntry c in codeNameList)
            //{
            //    if (c != null)
            //    {

            //        c.FAmount = !string.IsNullOrEmpty(c.FAmount.ToString()) ? c.FAmount / 10000 : 0;
            //    }
            //}
            return View(codeNameList);
        }
        public string Save(string FApplicationFormID, string Famount, string FNote, string FPayCertNo)
        {

            FApplicationFormID = Fetch.estimateString(FApplicationFormID, CheckGetEnum.Safety);
            Famount = Fetch.estimateString(Famount, CheckGetEnum.Safety);
            FNote = Fetch.estimateString(FNote, CheckGetEnum.Safety);
            FPayCertNo = Fetch.estimateString(FPayCertNo, CheckGetEnum.Safety);
            string FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string FEntityNo = GlobalConstant.ENTITYNO;
            string FInsIP = Request.UserHostAddress;
            DateTime FPayDay = DateTime.Now;
            Model.AllowancePayEntry model = new Model.AllowancePayEntry();
            BLL.AllowancePayEntry codeNameBll = new BLL.AllowancePayEntry();
            model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
            model.FNote = FNote;
            model.FApplicationFormID = FApplicationFormID;
            model.FAmount = Convert.ToDecimal(Famount);
          //  model.FAmount = Convert.ToDecimal(Famount) * 10000;
            model.FInsDate = FPayDay.ToLocalTime();
            model.FPayDate = FPayDay.ToLocalTime();
            model.FInsEmpNo = FInsEmpNo;
            model.FEntityNo = FEntityNo;
            model.FInsIP = FInsIP;
            model.FPayCertNo = FPayCertNo;
            try
            {

                codeNameBll.Add(model);
                return "ok";

            }
            catch
            {
                return "error";
            }

        }
        public string Del(string FId)
        {

            string str = string.Empty;
            if (null != FId)
            {
                FId = Fetch.estimateString(FId, CheckGetEnum.Safety);
                BLL.AllowancePayEntry codeNameBll = new BLL.AllowancePayEntry();
                try
                {
                    decimal fId = Convert.ToDecimal(FId);
                    codeNameBll.Delete(fId);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }
        public string AllowancePayEdits(string FApplicationFormID)
        {
            // string strwhere= str;
            //List<Model.AllowanceApplicationForm> codeNameList = new List<Model.AllowanceApplicationForm>();
            // ViewData["FApplicationFormID"] = FApplicationFormID;
            Session["FApplicationFormID"] = FApplicationFormID;
            BLL.AllowanceApplicationForm codeNameBll = new BLL.AllowanceApplicationForm();
            // codeNameList = codeNameBll.GetModelList1(strwhere);
            Model.AllowanceApplicationForm c = new Model.AllowanceApplicationForm();
            c = codeNameBll.GetModel1(FApplicationFormID);
            //c.FAuditAmount = !string.IsNullOrEmpty(c.FAuditAmount.ToString()) ? decimal.Parse(c.FAuditAmount.ToString()) / 10000 : 0;

            //c.Famount = !string.IsNullOrEmpty(c.Famount.ToString()) ? (decimal.Parse(c.Famount.ToString()) / 10000).ToString() : "";
            //c.FamountLeave = !string.IsNullOrEmpty(c.FamountLeave.ToString()) ? (decimal.Parse(c.FamountLeave.ToString()) / 10000).ToString() : "";
            return c.FamountLeaveStr.ToString() + ":" + c.Famount;
        }
    }
}
